package com.iscas.tech.service.impl;

import com.iscas.tech.bo.MdType;
import com.iscas.tech.entity.AtExperts;
import com.iscas.tech.entity.AtMdbusyValue;
import com.iscas.tech.mapper.AtExpertsMapper;
import com.iscas.tech.param.PageParam;
import com.iscas.tech.service.AtExpertsService;
import com.iscas.tech.service.AtMdbusyValueService;
import com.iscas.tech.utils.DataUtil;
import com.iscas.tech.vo.AtExpertsQueryVo;
import com.iscas.tech.vo.MdWithExpert;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 专家信息表 服务实现类
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@Service
public class AtExpertsServiceImpl extends BaseServiceImpl<AtExpertsMapper, AtExperts> implements AtExpertsService {

    @Autowired
    private AtExpertsMapper atExpertsMapper;

    @Autowired
    private AtMdbusyValueService mBusinessService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveAtExperts(MdWithExpert param) throws Exception {
        if (atExpertsMapper.saveExpert(param)) {
            if (param.getTagList() != null) {
                for (AtMdbusyValue atMdbusyValue : DataUtil.getBusinessList(param.getId(), param.getCreateTitle(), param.getTagList())) {
                    if (!mBusinessService.saveAtMdbusyValue(atMdbusyValue)) {
                        throw new Exception();
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAtExperts(MdWithExpert param) throws Exception {
        //  先修改基础信息
        if (!super.updateById(param.transform())) {
            throw new Exception();
        }
        //  删除业务模块信息
        mBusinessService.deleteByBsId(param.getId().longValue(), param.getEpType() == 1 ? MdType.TECH_EXPERT.getType() : MdType.TECH_TALENT.getType());
        //  新增业务模块信息
        if (param.getTagList() != null) {
            for (AtMdbusyValue atMdbusyValue : DataUtil.getBusinessList(param.getId(), param.getCreateTitle(), param.getTagList())) {
                if (!mBusinessService.saveAtMdbusyValue(atMdbusyValue)) {
                    throw new Exception();
                }
            }
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteExperts(String ids, Integer mdType) throws Exception {
        if (ids == null) {
            return true;
        }
        String[] idList = ids.split(",");
        for (String id : idList) {
            if (!atExpertsMapper.deleteExpert(Integer.parseInt(id), mdType)) {
                throw new Exception();
            }
        }
        return true;
    }

    @Override
    public Paging<AtExpertsQueryVo> getExpertOrTalentPageList(PageParam pageParam, Integer expertType, Integer audit) throws Exception {
        List<AtExpertsQueryVo> pageList = atExpertsMapper.getExpertOrTalentPageList(pageParam.getPageSize(), pageParam.getOffset(), expertType, audit);
        Paging paging = new Paging();
        paging.setRecords(pageList);
        paging.setTotal(atExpertsMapper.getExpertOrTalentCount(expertType, audit));
        return paging;
    }

    @Override
    public AtExpertsQueryVo get(Long id) {
        return atExpertsMapper.get(id);
    }

    @Override
    public Boolean checkExpertOrTalent(Long id) throws Exception {
        return atExpertsMapper.checkExpertOrTalent(id);
    }

}
